package com.fire.eyes.controller;



import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.util.BlogUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fire.core.base.controller.BaseController;
import com.fire.core.util.AjaxResult;
import com.fire.core.util.ToolUtil;
import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.model.TMitChannel;
import com.fire.eyes.model.TMitShelf;
import com.fire.eyes.service.ITMitChannelService;
import com.fire.eyes.service.ITMitShelfService;
import com.fire.eyes.service.ITMitThemeService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;


/**
 * 专题 控制器
 * @author raoyuxing
 *@date： 日期：2018年4月28日 
 *
 */
@RestController
@RequestMapping("/goods")
public class ThemeController extends BaseController {
	private static Logger LOGGER = Logger.getLogger(ThemeController.class);
	
	@Autowired
	private ITMitThemeService themeService;
	@Autowired
	private ITMitChannelService channelService;
	@Autowired
	private ITMitShelfService shelfService;
	@Autowired
	private ITMitErrorCodeService itMitErrorCodeService;
	
	
	/**
	 * 获取专题theme
	 * @param json
	 * @return
	 */
	@PostMapping(value="/getheme")
	@ResponseBody
	public AjaxResult getheme(@RequestBody String json){
    	LOGGER.info("ThemeController.getheme获取到的json消息" + json);
		BlogUtil.inlog(json,"","mit/goods/getheme","getheme","req");
		Map map = json2Map(json);
        AjaxResult ajaxResult = new AjaxResult();
		
		//效验渠道号是否为空和效验该渠道是否有效
		String channelid = "chanelid";
		if (ToolUtil.isEmpty(map.get(channelid))) {
	    	LOGGER.error("ThemeController.gethem.error:渠道号为空");
	    	ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "渠道号为空", "请输入渠道号"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme","getheme","resp");
			return ajaxResult;
		} else {
			EntityWrapper<TMitChannel> channelWrapper = new EntityWrapper<>();
			channelWrapper.eq("channelid", map.get("chanelid"))
						.eq("channelstatus", 1);
			int num = channelService.selectCount(channelWrapper);
			if (num == 0) {
		    	LOGGER.error("ThemeController.gethem.error:渠道号错误");
		    	ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "渠道号错误", "渠道号错误"));
				BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme","getheme","resp");
				return ajaxResult;
			}
		}
		
		//效验主题号是否有效
		String shelfid = "shelfid";
		if (ToolUtil.isNotEmpty(map.get(shelfid))) {
			EntityWrapper<TMitShelf> shelfWrapper = new EntityWrapper<>();
			shelfWrapper.eq("shelfid", map.get("shelfid"))
						.eq("status", 1);
			int num = shelfService.selectCount(shelfWrapper);
			if (num == 0) {
		    	LOGGER.error("ThemeController.gethem.error:货架或者主题号错误");
		    	ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "货架或者主题号错误", "货架或者主题号错误"));
				BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme","getheme","resp");
				return ajaxResult;
			}
		}
		Map result = null;
		try {
			result = themeService.getheme(map);
		} catch (Exception e) {
			LOGGER.error("ThemeController.getheme().error  错误为：",e);
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SYS_DATAERROR,"系统错误","系统错误"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme","getheme","resp");
			return ajaxResult;
		}
		ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",result, "操作成功"));
		BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme","getheme","resp");
		return ajaxResult;
	}

	/**
	 * 获取专题theme
	 * @param json
	 * @return
	 */
	@PostMapping(value="/getheme2")
	@ResponseBody
	public AjaxResult getTheme2(@RequestBody String json){
		LOGGER.info("ThemeController.getTheme2获取到的json消息" + json);
		BlogUtil.inlog(json,"","mit/goods/getheme2","getTheme2","req");
		Map map = json2Map(json);
		AjaxResult ajaxResult = new AjaxResult();

		//效验渠道号是否为空和效验该渠道是否有效
		String channelid = "chanelid";
		if (ToolUtil.isEmpty(map.get(channelid))) {
			LOGGER.error("ThemeController.getTheme2.error:渠道号为空");
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "渠道号为空", "请输入渠道号"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme2","getTheme2","resp");
			return ajaxResult;
		} else {
			EntityWrapper<TMitChannel> channelWrapper = new EntityWrapper<>();
			channelWrapper.eq("channelid", map.get("chanelid"))
					.eq("channelstatus", 1);
			int num = channelService.selectCount(channelWrapper);
			if (num == 0) {
				LOGGER.error("ThemeController.getTheme2.error:渠道号错误");
				ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "渠道号错误", "渠道号错误"));
				BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme2","getheme","resp");
				return ajaxResult;
			}
		}

		//效验主题号是否有效
		String shelfid = "shelfid";
		if (ToolUtil.isNotEmpty(map.get(shelfid))) {
			EntityWrapper<TMitShelf> shelfWrapper = new EntityWrapper<>();
			shelfWrapper.eq("shelfid", map.get("shelfid"))
					.eq("status", 1);
			int num = shelfService.selectCount(shelfWrapper);
			if (num == 0) {
				ajaxResult  = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.GOODS_PARAMERROR, "货架或者主题号错误", "货架或者主题号错误"));
				LOGGER.error("ThemeController.getTheme.error:货架或者主题号错误");
				BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme2","getTheme2","resp");
				return ajaxResult;
			}
		}
		Map result = null;
		try {
			result = themeService.getTheme2(map);
		} catch (Exception e) {
			LOGGER.error("ThemeController.getTheme().error  错误为：",e);
			ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SYS_DATAERROR,"系统错误","系统错误"));
			BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme2","getTheme2","resp");
			return ajaxResult;
		}
		ajaxResult = itMitErrorCodeService.replaceMess(ajaxResult(ErrorCodeConstants.SUCCESS, "操作成功",result, "操作成功"));
		BlogUtil.inlog(JSON.toJSONString(ajaxResult),"","mit/goods/getheme2","getTheme2","resp");
		return ajaxResult;
	}
	
}
